package chap05;

/**
 * Created by root on 2017/7/7.
 */
class SortedList{

    public Link1 first;

    public boolean isEmpty(){
        return first==null;
    }

    public void insert(long key){
        Link1 newLink = new Link1(key);
        Link1 current = first;
        Link1 previous = null;
        while (current!=null && key > current.dData) {
            previous = current;
            current = current.next;
        }
        if (previous == null){
            first = newLink;
        } else {
            previous.next = newLink;
        }
        newLink.next = current;
    }

    public Link1 remove(){
        Link1 temp = first;
        first = first.next;
        return temp;
    }

    public void display(){
        System.out.print("List (first-->last): ");
        Link1 current = first;
        while (current!=null) {
            current.displayLink();
            current = current.next;
        }
        System.out.println(" ");
    }

}
public class SortListApp {
    public static void main(String[] args){
        SortedList theSortedList = new SortedList();
        theSortedList.insert(20);
        theSortedList.insert(40);

        theSortedList.display();

        theSortedList.insert(10);
        theSortedList.insert(30);
        theSortedList.insert(50);

        theSortedList.display();

        theSortedList.remove();

        theSortedList.display();

    }
}
